#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QClipboard>
#include <QMimeData>
#include "css_decoder.h"
#include "widget_encoder.h"
#include <QVBoxLayout>
#include "textwidget.h"
#include <QMessageBox>
#include <QCursor>
#include "intlwidget.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), textPopup(nullptr)
{
    resize(500, 500);
    cWidget = new QWidget(this);
    container = new QVBoxLayout(cWidget);
    textWidget = new TextWidget(cWidget);
    tabWidget = new QTabWidget(cWidget);
    tabWidget->addTab(textWidget, "Text");

    intlWidget = new IntlWidget(cWidget);
    tabWidget->addTab(intlWidget,"Intl");

    container->addWidget(tabWidget);
    setCentralWidget(cWidget);
    clipboard_ = QGuiApplication::clipboard();
    connect_clipboard();
}

MainWindow::~MainWindow()
{
}
void MainWindow::connect_clipboard() const noexcept
{
    connect(clipboard_, &QClipboard::dataChanged, this, &MainWindow::on_clipboard_datachanged);
}

void MainWindow::on_clipboard_datachanged()
{
    // textPopup = new TextPopup(this, "");
    // textPopup->show();
    // return;

    auto clipboard_text = clipboard_->text().toStdString();
    if (clipboard_text.empty())
    {
        return;
    }
    qDebug() << "on_clipboard_datachanged";
    dymy::CSSDecoder decoder(0.5);
    auto t = decoder.Decode(clipboard_text);

    if (!t)
    {
        if (lastText_)
        {
            lastText_->text = clipboard_text;
            qDebug() << "set clipboard success:";
        }
        else
        {
            return;
        }
    }
    else if (t->type == dymy::WidgetType::Text)
    {
        lastText_.reset(dynamic_cast<dymy::Text *>(t));
        if (lastText_->font_family && !textWidget->isFontFamilyChecked())
        {
            lastText_->font_family.reset();
        }
        qDebug() << "generate text success";
    }
    else if (t->type == dymy::WidgetType::Container)
    {
        return;
    }
    else
    {
        return;
    }

    textPopup = new TextPopup(this, lastText_.get());
    textPopup->setWindowFlag(Qt::Popup);
    textPopup->setAttribute(Qt::WA_DeleteOnClose);
    QPoint pos = QCursor::pos();
    textPopup->setGeometry(pos.x()-300, pos.y(), 300, 50);
    connect(textPopup, &TextPopup::onClosed, [this](TextPopup::Type type)
            {
                switch (type) {
                    case TextPopup::Type::kCancel:
                        lastText_.reset();
                        break;
                    case TextPopup::Type::kContinue:

                        break;
                    case TextPopup::Type::kCopyStyle:
                        lastText_.reset();
                        break;
                    case TextPopup::Type::kCopyText:
                        lastText_.reset();
                        break;
                }
                connect_clipboard();
            });

    disconnect(clipboard_, &QClipboard::dataChanged, this, &MainWindow::on_clipboard_datachanged);
    textPopup->show();
}